package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.model.BugType;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.BugDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.BugService;
import com.suyang.syt.service.interfaces.BugTypeService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.JsonUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/bug")
@AuthClass
public class AdminBugController {

	@Autowired
	private BugService bugService;
	@Autowired
	private BugTypeService bugTypeService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "bug/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<BugDTO> list(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			String checkName = request.getParameter("checkName");
			String checkBtid = request.getParameter("checkBtid");
			String checkStatus = request.getParameter("checkStatus");
			return bugService.listByEasyUI(page, rows, sort, order, createDate1, createDate2, checkName, checkBtid, checkStatus);
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/findBugType", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj findBugType(HttpServletRequest request) {
		try {
			ServletContext servletContext = request.getSession().getServletContext();
			List<BugType> list = (List<BugType>) servletContext.getAttribute("listBugType");
			if (list == null) {
				list = bugTypeService.findBugType();
				servletContext.setAttribute("listBT", JsonUtil.toJson(list));
			}
			return new AjaxObj(1, "展位类别加载成功！", list);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, BugDTO bugDTO) {
		User user = (User) request.getSession().getAttribute("loginUser");
		try {
			bugDTO.setCreateName(user.getUsername());
			bugDTO.setCreateTime(DateFormat.dateToString(new Date()));
			return bugService.add(bugDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return bugService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, BugDTO bugDTO) {
		User user = (User) request.getSession().getAttribute("loginUser");
		try {
			bugDTO.setModifyName(user.getUsername());
			bugDTO.setModifyTime(DateFormat.dateToString(new Date()));
			return bugService.update(bugDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
